package com.ls.rbac.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * created by liush on 2025/6/28
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //于实现 MyBatis-Plus 的自动填充功能：insertFill：在插入数据时，自动填充 createTime 和 updateTime 字段为当前时间。
    //updateFill：在更新数据时，自动更新 updateTime 字段为当前时间
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
